package com.itheima;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {
    private Airplane airplane;
    private List<Bullet> bullets;
    private List<Monster> monsters;
    private Timer gameTimer;

    private boolean leftPressed, rightPressed, spacePressed;
    private int score;
    private boolean gameOver;

    public GamePanel() {
        airplane = new Airplane(375, 500);
        bullets = new ArrayList<>();
        monsters = new ArrayList<>();
        score = 0;
        gameOver = false;

        // 添加键盘监听
        addKeyListener(this);
        setFocusable(true);

        // 游戏定时器
        gameTimer = new Timer(30, this); // 每30毫秒更新一次
        gameTimer.start();

        // 生成怪兽的定时器
        new Timer(1500, e -> {
            if (!gameOver) {
                monsters.add(new Monster(new Random().nextInt(700) + 50, 50));
            }
        }).start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 绘制背景
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());

        // 绘制地面
        g.setColor(Color.GRAY);
        g.fillRect(0, 550, 800, 50);

        // 设置字体为Arial，解决中文显示问题
        g.setFont(new Font("Arial", Font.BOLD, 20));
        g.setColor(Color.WHITE);
        g.drawString("Score: " + score, 10, 30);

        // 如果游戏结束，显示结束信息
        if (gameOver) {
            g.setColor(Color.RED);
            g.setFont(new Font("Arial", Font.BOLD, 40));
            g.drawString("Game Over!", 300, 300);
            g.setFont(new Font("Arial", Font.BOLD, 24));
            g.drawString("Final Score: " + score, 320, 350);
            g.drawString("Press R to Restart", 300, 400);
        }

        // 绘制飞机
        airplane.draw(g);

        // 绘制子弹
        for (Bullet bullet : bullets) {
            bullet.draw(g);
        }

        // 绘制怪兽
        for (Monster monster : monsters) {
            monster.draw(g);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (gameOver) {
            return;
        }

        // 更新飞机位置
        if (leftPressed && airplane.getX() > 0) {
            airplane.moveLeft();
        }
        if (rightPressed && airplane.getX() < 700) {
            airplane.moveRight();
        }
        if (spacePressed) {
            shoot();
            spacePressed = false; // 防止连续发射
        }

        // 更新子弹位置，检查子弹是否超出边界
        for (Iterator<Bullet> bulletIter = bullets.iterator(); bulletIter.hasNext();) {
            Bullet bullet = bulletIter.next();
            bullet.moveUp();

            if (bullet.getY() < 0) {
                bulletIter.remove();
            }
        }

        // 更新怪兽位置，检查怪兽是否超出边界
        for (Iterator<Monster> monsterIter = monsters.iterator(); monsterIter.hasNext();) {
            Monster monster = monsterIter.next();
            monster.moveDown();

            // 检查怪兽与飞机碰撞
            if (monster.collidesWith(airplane)) {
                gameOver = true;
                JOptionPane.showMessageDialog(this, "游戏结束! 得分: " + score, "游戏结束", JOptionPane.INFORMATION_MESSAGE);
                retryGame();
                return;
            }

            if (monster.getY() > 600) {
                monsterIter.remove();
            }
        }

        // 检查子弹与怪兽碰撞
        for (Iterator<Bullet> bulletIter = bullets.iterator(); bulletIter.hasNext();) {
            Bullet bullet = bulletIter.next();
            for (Iterator<Monster> monsterIter = monsters.iterator(); monsterIter.hasNext();) {
                Monster monster = monsterIter.next();
                if (bullet.collidesWith(monster)) {
                    monsterIter.remove();
                    bulletIter.remove();
                    score += 10;
                    break; // 跳出内层循环，处理下一个子弹
                }
            }
        }

        repaint();
    }
    private void shoot() {
        bullets.add(new Bullet(airplane.getX() + 25, airplane.getY() - 10));
    }

    private void retryGame() {
        airplane = new Airplane(375, 500);
        bullets.clear();
        monsters.clear();
        score = 0;
        gameOver = false;
        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // 不需要实现
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (gameOver) {
            if (e.getKeyCode() == KeyEvent.VK_R) {
                retryGame();
            }
            return;
        }

        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            leftPressed = true;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            rightPressed = true;
        } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            spacePressed = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            leftPressed = false;
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            rightPressed = false;
        }
    }
}